#### II Replication ####
### purpose: replicating table 1 ####
# installing packages ----

list.of.packages = c('estimatr', 'stargazer', 'MASS')
new.packages <- list.of.packages[!(list.of.packages %in% 
                                     installed.packages()[,"Package"])]

if(length(new.packages)) install.packages(new.packages)

# loading packages ----

suppressPackageStartupMessages(
  {
    library(estimatr)
    library(stargazer)
    library(MASS)
  }
)

# loading in data ----
dt <- readRDS("~/rep_data.RDS")

# Table 1 ----
# First Row ----
lm1 <- lm(fascore ~ modi0_num_contract, data=dt)

lm1_se <- lm_robust(fascore ~ modi0_num_contract, data=dt, se_type = "HC2")

negbino1 <- glm.nb(fascore ~ modi0_num_contract, data = dt)

lm2 <- lm(fascore ~ modi0_num_contract + 
            highskill + highedu + 
            medianincome + unemploy + 
            black + white + hispanic + foreignborn
          +  EvanProt + Catholic + MainProt
          + nominate1 + app + foreign  + 
            labor + corp + bank + prezSupport 
          + as.factor(state), data = dt)

lm2_se <- lm_robust(fascore ~ modi0_num_contract + 
                      highskill + highedu + 
                      medianincome + unemploy + 
                      black + white + hispanic + foreignborn
                    +  EvanProt + Catholic + MainProt
                    + nominate1 + app + foreign  + 
                      labor + corp + bank + prezSupport, fixed_effects = ~ as.factor(state), data=dt, clusters = as.factor(state), se_type = "CR2")

negbino2 <- glm.nb(fascore ~ modi0_num_contract + 
                     highskill + highedu + 
                     medianincome + unemploy + 
                     black + white + hispanic + foreignborn
                   +  EvanProt + Catholic + MainProt
                   + nominate1 + app + foreign  + 
                     labor + corp + bank + prezSupport 
                   + as.factor(state), data = dt)

lm3 <- lm(fascore ~ modi0_num_contract + 
            highskill + highedu + 
            medianincome + unemploy + 
            black + white + hispanic + foreignborn
          +  EvanProt + Catholic + MainProt
          + nominate1 + app + foreign  + 
            labor + corp + bank + prezSupport 
          + as.factor(state) + as.factor(cong), 
          data = dt)

lm3_se <- lm_robust(fascore ~ modi0_num_contract + 
                      highskill + highedu + 
                      medianincome + unemploy + 
                      black + white + hispanic + foreignborn
                    +  EvanProt + Catholic + MainProt
                    + nominate1 + app + foreign  + 
                      labor + corp + bank + prezSupport, fixed_effects = ~ state + cong, data=dt, 
                    clusters = state, se_type = "CR2")


negbino3 <- glm.nb(fascore ~ modi0_num_contract + 
                     highskill + highedu + 
                     medianincome + unemploy + 
                     black + white + hispanic + foreignborn
                   +  EvanProt + Catholic + MainProt
                   + nominate1 + app + foreign  + 
                     labor + corp + bank + prezSupport 
                   + as.factor(state) + as.factor(cong), 
                   data = dt)

stargazer(lm1, lm2,  lm3, negbino1, negbino2, negbino3,
          model.names =TRUE,
          se = starprep(lm1_se, lm2_se, lm3_se),
          header=FALSE,
          keep=c("modi0_num_contract"),
          covariate.labels=c("Contract Num"), 
          align=T,
          font.size="scriptsize",
          omit.stat=c("f", "ser", "bic", "aic", "ll", "theta"),
          column.sep.width="0pt",
          dep.var.labels = "Dependent Variable: FA Score (The number of pro-foreign aid bills each representative cosponsored)",
          omit.table.layout="l",
          type="latex",
          add.lines = list( c("state FE", "N", "Y",  "Y",  "N", "Y", "Y"), c("cong FE", "N",  "N", "Y", "N", "N", "Y"), c("SE cluster", "-",  "state", "state", "-", "-", "-")))


# Second Row ----
lm1 <- lm(fascore ~ modi0_initial_award, data=dt)

lm1_se <- lm_robust(fascore ~ modi0_initial_award, data=dt, se_type = "HC2")

negbino1 <- glm.nb(fascore ~ modi0_initial_award, data = dt)

lm2 <- lm(fascore ~ modi0_initial_award + 
            highskill + highedu + 
            medianincome + unemploy + 
            black + white + hispanic + foreignborn
          +  EvanProt + Catholic + MainProt
          + nominate1 + app + foreign  + 
            labor + corp + bank + prezSupport 
          + as.factor(state), data = dt)

lm2_se <- lm_robust(fascore ~ modi0_initial_award + 
                      highskill + highedu + 
                      medianincome + unemploy + 
                      black + white + hispanic + foreignborn
                    +  EvanProt + Catholic + MainProt
                    + nominate1 + app + foreign  + 
                      labor + corp + bank + prezSupport, fixed_effects = ~ as.factor(state), data=dt, clusters = as.factor(state), se_type = "CR2")

negbino2 <- glm.nb(fascore ~ modi0_initial_award + 
                     highskill + highedu + 
                     medianincome + unemploy + 
                     black + white + hispanic + foreignborn
                   +  EvanProt + Catholic + MainProt
                   + nominate1 + app + foreign  + 
                     labor + corp + bank + prezSupport 
                   + as.factor(state), data = dt)

lm3 <- lm(fascore ~ modi0_initial_award + 
            highskill + highedu + 
            medianincome + unemploy + 
            black + white + hispanic + foreignborn
          +  EvanProt + Catholic + MainProt
          + nominate1 + app + foreign  + 
            labor + corp + bank + prezSupport 
          + as.factor(state) + as.factor(cong), 
          data = dt)

lm3_se <- lm_robust(fascore ~ modi0_initial_award + 
                      highskill + highedu + 
                      medianincome + unemploy + 
                      black + white + hispanic + foreignborn
                    +  EvanProt + Catholic + MainProt
                    + nominate1 + app + foreign  + 
                      labor + corp + bank + prezSupport, fixed_effects = ~ state + cong, data=dt, 
                    clusters = state, se_type = "CR2")


negbino3 <- glm.nb(fascore ~ modi0_initial_award + 
                     highskill + highedu + 
                     medianincome + unemploy + 
                     black + white + hispanic + foreignborn
                   +  EvanProt + Catholic + MainProt
                   + nominate1 + app + foreign  + 
                     labor + corp + bank + prezSupport 
                   + as.factor(state) + as.factor(cong), 
                   data = dt)

stargazer(lm1, lm2,  lm3, negbino1, negbino2, negbino3,
          model.names =TRUE,
          se = starprep(lm1_se, lm2_se, lm3_se),
          header=FALSE,
          keep=c("modi0_initial_award"),
          covariate.labels=c("Award Amount"), 
          align=T,
          font.size="scriptsize",
          omit.stat=c("f", "ser", "bic", "aic", "ll", "theta"),
          column.sep.width="0pt",
          dep.var.labels = "Dependent Variable: FA Score (The number of pro-foreign aid bills each representative cosponsored)",
          omit.table.layout="l",
          type="latex",
          add.lines = list( c("state FE", "N", "Y",  "Y",  "N", "Y", "Y"), c("cong FE", "N",  "N", "Y", "N", "N", "Y"), c("SE cluster", "-",  "state", "state", "-", "-", "-")))